﻿package cn.antscript.FaceDetector 
{
	import cn.antscript.FaceDetector.view.component.GlassesDetector;
	import cn.antscript.FaceDetector.view.component.PhotoPanel;
	import cn.antscript.FaceDetector.view.component.ShowPanel;
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.geom.Matrix;
	
	/**
	 * ...
	 * @author AntScript
     * @blog  http://www.antscript.cn
	 */
	
	[SWF(width="640",height="480",frameRate="24")]
	
	public class Main extends Sprite
	{
		private var _glassesDetector:GlassesDetector;
		private var _photoPanel:PhotoPanel;
		private var _showPanel:ShowPanel;
		
		public function Main() 
		{
			init();
		}
		
		private function init():void
		{
			initGlassesDetector();
			initPhotoPanel();
			
			stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
		}
		
		private function onKeyDown(e:KeyboardEvent):void 
		{
			var bitmapData:BitmapData = new BitmapData(320, 240, false);
			bitmapData.draw(_glassesDetector);
			var bitmap:Bitmap = new Bitmap(bitmapData);
			_photoPanel.addPhoto(bitmap);
		}
		
		private function initGlassesDetector():void
		{
			_glassesDetector = new GlassesDetector();
			_glassesDetector.x = 160;
			_glassesDetector.y = 20;
			addChild(_glassesDetector);
			
			
		}
		
		private function initPhotoPanel():void
		{
			_photoPanel = new PhotoPanel();
			_photoPanel.y = 360;
			addChild(_photoPanel);
			_photoPanel.addEventListener(Event.CHANGE, onChange);
			
			_showPanel = new ShowPanel();
			_showPanel.x = 160;
			_showPanel.y = 20;
			addChild(_showPanel);
			_showPanel.addEventListener(MouseEvent.CLICK, onClick);
			_showPanel.visible = false;
		}
		
		private function onClick(e:MouseEvent):void 
		{
			_showPanel.visible = false;
		}
		
		private function onChange(e:Event):void 
		{
			_showPanel.visible = true;
			_showPanel.show(_photoPanel.bitmapdata);
		}
		
		
		
	}
	
}